var listType = null;
var goodId =null;
var state = 0;
var count = 0;
var uid = null;

$(document).ready(function() {
	getMyGoods();
	
	$("#add").hover(function(){  
        $("#dwon").slideDown();  
    },function(){  
        $("#dwon").slideUp(); 
    }); 
});

function getAll() {
	listType = "";
	getMyGoods();
}

function getSale() {
	listType = "sale";
	getMyGoods();
}

function getBuy() {
	listType = "buy";
	getMyGoods();
}

function getMyGoods() {
	$.ajax({
		type : "post",
		async : false,
		url : "getMyGoods.action",
		dataType : "json",
		data : {
			"listType" : listType
		},
		success : handel
	});
}


var content="";
function handel(data) {
	content = data;
	var put = "";
	var put1 = "<h3>正在进行中</h3>";
	var put2 = "<h3>已交易成功</h3>";
	var put3 = "<h3>已下架</h3>";
	if (data == null) {
		put = '<h3>没有您的发布信息</h3>';
	} else{
		for ( var i = 0; i < data.length; i++) {	
		if(data[i].state==1){
			put1 += '<div class="good"><table border="1"><tr>';
			if(data[i].listType=="sale"){
				put1 += '<td rowspan="2" style="width:80px"><img src="uImg/'
					+ data[i].goodId
					+ '.jpg"></td>';
			}
			put1 += '<td>物品名</td><td>发布时间</td> <td>价格</td><td>状态</td> <td>操作</td>';
			put1 += '</tr> <tr><td>' + data[i].goodName + '</td> <td>'
					+ data[i].createDate + '</td><td>' + data[i].price
					+ ' RMB</td><td>待售中</td><td><button onclick="javascript:updateGood(\''
					+ data[i].goodId + '\')">编辑</button>&nbsp;&nbsp;&nbsp;';
			put1 += '</td> </tr></table> </div>';
		}else if(data[i].state==2){
			put2 += '<div class="good"><table border="1"><tr>';
			if(data[i].listType=="sale"){
				put2 += '<td rowspan="2" style="width:80px"><img src="uImg/'
					+ data[i].goodId
					+ '.jpg"></td>';
			}
			put2 += '<td>物品名</td><td>发布时间</td> <td>价格</td><td>状态</td> <td>操作</td>';
			put2 += '</tr> <tr><td>' + data[i].goodName + '</td> <td>'
					+ data[i].createDate + '</td><td>' + data[i].price
					+ ' RMB</td><td>已交易成功</td><td><button onclick="javascript:updateGood(\''
					+ data[i].goodId + '\')">编辑</button>&nbsp;&nbsp;&nbsp;';
			put2 += '</td> </tr></table> </div>';	
	  }else{
		  put3 += '<div class="good"><table border="1"><tr>';
			if(data[i].listType=="sale"){
				put3 += '<td rowspan="2" style="width:80px"><img src="uImg/'
					+ data[i].goodId
					+ '.jpg"></td>';
			}
			put3 += '<td>物品名</td><td>发布时间</td> <td>价格</td><td>状态</td> <td>操作</td>';
			put3 += '</tr> <tr><td>' + data[i].goodName + '</td> <td>'
					+ data[i].createDate + '</td><td>' + data[i].price
					+ ' RMB</td><td>已下架</td><td><button onclick="javascript:updateGood(\''
					+ data[i].goodId + '\')">编辑</button>&nbsp;&nbsp;&nbsp;';
			put3 += '</td> </tr></table> </div>';
	    }
	  }
		if(put1=="<h3>正在进行中</h3>"){
			put1+="<p>当前没有正在进行中的发布信息</p>";
		}
		if(put2=="<h3>已交易成功</h3>"){
			put2+="<p>当前没有交易成功的交易</p>";
		}
		if(put3=="<h3>已下架</h3>"){
			put3+="<p>当前没有已下架的发布</p>";
		}
		put = put1+put2+put3;
	}
	
	document.getElementById("here").innerHTML = put;
}




function updateGood(gId){
	goodId = gId;
	for(var i = 0;i<content.length;i++){
		var id = content[i].goodId;
		if(goodId==id){
	        uid = content[i].uid;
			state = content[i].state;
			count = content[i].count;
			listType = content[i].listType;
			if(state==3){
				$("#type3").hide();
				$("#type1").attr("value","重   新   上   架");
			}else if(state==1){
				$("#type3").show();
				$("#type1").attr("value","更           新");
			}
			if(content[i].listType=="buy"){
				$("#p02").hide();
				$("#pic").hide();
				$("#ms4").hide();
			}else{
				$("#p02").show();
				$("#pic").show();
				$("#ms4").show();
				$("#pic").attr("src","uImg/"+id+".jpg");
			}
			
			
			$("#name").attr("value",content[i].goodName);
			$("#types").val(content[i].type);		
			$("#count").text(content[i].count);
			$("#detail").text(content[i].briefing);
			$("#price").attr("value",content[i].price);
		}
	}

	$(".up").fadeIn("slow");
}


$(document).ready(function() {
	$("#de").click(function() {
		count = count - 1;
		if(count<=0){
		    count=0;
		    $("#ms5").text("数量不能为零");
		}
		$("#count").text(count);
	});
	$("#ad").click(function() {
	    $("#ms5").text("");
		count = count*1 + 1*1;
		$("#count").text(count);
	});
	
	
	var errorMsg = "输入內容不符合要求";
    var rightMsg = "符合要求";
    var nameMsg = rightMsg;
	var detailMsg = rightMsg;
	var priceMsg = rightMsg;

$("#name").blur(function() {
		var name = $("#name").val();
		var checkName = /[\u4e00-\u9fa5_a-zA-Z0-9_]{1,12}/;
		if(checkName.test(name)==false){
		     nameMsg = errorMsg;
		    $("#ms1").text(nameMsg);
		}else{
		   nameMsg = rightMsg;
		   $("#ms1").text(nameMsg);
		}
	});

    $("#ms2").text(detailMsg);

$("#price").blur(function(){
      var price = $("#price").val();
      var checkPrice=/^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$/;
      if(checkPrice.test(price)==false){
		    priceMsg = errorMsg ;
		    $("#ms3").text(priceMsg);
		}else{
		   priceMsg = rightMsg;
		   $("#ms3").text(priceMsg);
		}
});

 $("#type1").click(function(){
     if(nameMsg==errorMsg||priceMsg==errorMsg||count==0){
           alert("存在輸入不符合，請重新輸入");
     }else{
    	var post = 'updateGood.action?good.listType='+listType+'&good.state=1&good.count='+count+'&good.goodId='+goodId;
 		$("#mss").attr("action",post);
        $("#mss").submit();
     }
});

$("#type2").click(function() {
		$(".up").fadeOut("slow");
	});

$("#type3").click(function() {
	window.location.href='downGood.action?good.goodId='+goodId;
});

//上传图片

 	$(function() {
	$("#pic").click(function() {
		
		$("#upload").on("change", function() {
		    var imgSize =this.files[0].size;
			 if(imgSize<1024*1024*2){
                   $("#ms4").text("图片符合");	
                   var objUrl = window.URL.createObjectURL(this.files[0]); 
				   $("#pic").attr("src", objUrl); 		
             }else{
                   $("#pic").attr("src", "img/addjpg.jpg"); 
                   $("#ms4").text("图片大小在2M以外，为："+imgSize/(1024*1024)+"M，请重新选择");
             }
		});
		
		$("#upload").click(); 
    });
});

});


